package cn.jbooter.repository.autoconfigure.first;

import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import cn.jbooter.mybatis.annotation.Ds1Mapper;
import cn.jbooter.mybatis.mapping.IBaseMapper;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

/**
 * 通用mapper配置
 * @author HeJian
 *
 */
@Configuration
@AutoConfigureAfter(FirstMybatisConfigure.class)
@ConditionalOnProperty(prefix="spring.datasource.druid.first",name="enabled",havingValue="true")
public class FirstMapperScannerConfigure {
	private static final Logger logger = LoggerFactory.getLogger(FirstMapperScannerConfigure.class);
	
	/**
	 * 集成通用mapper
	 * 注意:
	 * 		2,必须使用tk.mybatis.spring.mapper.MapperScannerConfigurer 用于mapper包的扫描
	 * 		3,不使用通用mapper请改为:org.mybatis.spring.mapper.MapperScannerConfigurer 
	 * 
	 * @return
	 */
	@Primary
	@Bean
	public static MapperScannerConfigurer firstMapperScannerConfigurer() {
		logger.info("===>>>初始化主数据源mybatis的mapperScanner.");
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		mapperScannerConfigurer.setSqlSessionFactoryBeanName("firstSqlSessionFactory");
		mapperScannerConfigurer.setBasePackage("com.**.mapper,cn.**.mapper,org.**.mapper");
		mapperScannerConfigurer.setAnnotationClass(Ds1Mapper.class);
		//mapperScannerConfigurer.setMarkerInterface(Mapper.class);//直接继承了Mapper接口的才会被扫描
       
		//通用mapper配置
		Properties properties = new Properties();
		// 这里要特别注意，不要把IBaseMapper放到 basePackage 中，也就是不能同其他Mapper一样被扫描到
		properties.setProperty("mappers", IBaseMapper.class.getName()); //自定义mapper配置
		//主键自增回写方法,默认值CALL IDENTITY(),适应于大多数数据库
		//properties.setProperty("IDENTITY", "MYSQL");
		properties.setProperty("notEmpty", "true");
		//这里特别注意大小写 主键自增回写方法执行顺序,默认AFTER,可选值为(BEFORE|AFTER)
		properties.setProperty("ORDER", "BEFORE");
		mapperScannerConfigurer.setProperties(properties);
		return mapperScannerConfigurer;
	}

	
	
}
